import {renameSync,existsSync} from "fs";
import {resolve} from "path";
export default function RenamePlugin(moduleName){
    let root = "";
    let outDir = "";
    return {
        name:"rollup-plugin-rename",
        configResolved(config){
            // console.log(config)
            root = config.root;
            outDir = config.build.outDir;
        },
        closeBundle(){
            resoveModuleName(root);
            let originPath = resolve(root,outDir,"index.html");
            let targetModuleName = moduleName || resoveModuleName(root);
            let targetPath = resolve(root,outDir,targetModuleName+".html");
            console.log(originPath)
            if(!existsSync(originPath)) return;

            renameSync(originPath,targetPath)
        }
    }
}

function resoveModuleName(root){
   let pathArr = root.split("/");
    // console.log(pathArr)
    return pathArr[pathArr.length-1];
};